package handler

import (
	"context"
	"fmt"
	"server/basic/global"
	"server/models"
	__ "server/proto"
)

type Server struct {
	__.UnimplementedUserServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) Login(_ context.Context, in *__.LoginRequest) (*__.LoginResponse, error) {
	var find models.Users
	global.DB.Where("tel = ?", in.Tel).Find(&find)
	if in.Tel == "" {
		return nil, fmt.Errorf("用户未注册")
	}
	if in.Tel != find.Tel || in.Password != find.Password {
		return nil, fmt.Errorf("用户或者密码错误")
	}
	return &__.LoginResponse{
		Id: find.Id,
	}, nil
}
func (s *Server) List(_ context.Context, in *__.ListRequest) (*__.ListResponse, error) {
	var find []models.Ticket
	global.DB.Where("status like ?").Find(&find)
	var finds []*__.List
	for _, v := range find {
		list := &__.List{
			Yupiao: int64(v.Yupiao),
			Status: int64(v.Status),
			Price:  float32(v.Price),
			Zuo:    v.Zuo,
		}
		finds = append(finds, list)
	}
	return &__.ListResponse{
		List: finds,
	}, nil
}
func (s *Server) Add(_ context.Context, in *__.AddRequest) (*__.AddResponse, error) {
	var find models.Ticket
	if find.Status == 3 {
		return nil, fmt.Errorf("无座 现不可购票")
	}
	add := models.Ticket{
		Yupiao: int32(in.Yupiao),
		Status: int32(in.Status),
		Price:  float64(in.Price),
		Zuo:    in.Zuo,
	}
	global.DB.Create(&add)
	return &__.AddResponse{
		Id: int64(add.Id),
	}, nil
}
func (s *Server) Info(_ context.Context, in *__.InfoRequest) (*__.InfoResponse, error) {
	var find models.Ticket
	global.DB.Where("id = ?", in.Id).Find(&find)
	if find.Id == 0 {
		return nil, fmt.Errorf("订单不存在 不能查看")
	}
	finds := models.Ticket{
		Id: int32(in.Id),
	}

	return &__.InfoResponse{
		Zuo:    finds.Zuo,
		Yupiao: int64(finds.Yupiao),
		Status: int64(finds.Status),
		Price:  float32(finds.Price),
	}, nil
}
